package com.ptswitch.material.pages.bom;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;
import org.hxzon.project.AppRuntimeException;
import org.slf4j.Logger;
import org.springframework.dao.DataAccessException;

import com.ptswitch.material.model.material.Bom;
import com.ptswitch.material.model.material.BomOrigFile;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.BomService;
import com.ptswitch.material.service.MaterialExcelService;
import com.ptswitch.material.util.HlogConstants;
import com.ptswitch.material.util.WebUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class EditBom extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private MaterialExcelService excelSer;
    @Inject
    private BomService bomSer;
    @Property
    private Bom bom;
    @Property
    private UploadedFile file;
    @InjectPage
    private ViewBom viewBom;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        bom = bomSer.findById(id);
        checkPermitted(PermissionUtil.BomEdit);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return bom.getId();
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "update")
    Object updateBom() {
        checkPermitted(PermissionUtil.BomEdit);
        // check other same name bom
        Bom dbBom = bomSer.findByCodeAndVersion(bom.getCode(), bom.getVersion());
        if (dbBom != null && dbBom.getId() != bom.getId()) {
            throw AppRuntimeException.error("修改单板表不成功：已存在其它同名同版本号的单板表，请点击修改。");
        }
        // excel
        BomOrigFile upload = WebUtil.upload(file, BomOrigFile.class);
        if (upload == null) {
            bomSer.save(bom);
            hlog(HlogConstants.updateBom, bom);
            return viewBom.withInit(bom.getId());
        }
        // update upload
        Bom updateBom = new Bom();
        updateBom.setCode(bom.getCode());
        updateBom.setVersion(bom.getVersion());
        updateBom.setRemark(bom.getRemark());
        try {
            excelSer.buildBomFromExcel(updateBom, WebUtil.getRealFilePath(upload));
        } catch (Throwable e) {
            WebUtil.delete(upload);
            throw AppRuntimeException.rethrow("读取单板表文件错误", e);
        }
        // save db
        try {
            upload.setUser(getVisitor());
            updateBom.setId(0);
            updateBom.setFileName(upload.getFilePath());
            updateBom.setOrigFile(upload);
            bomSer.update(bom, updateBom);
            bom = updateBom;
            hlog(HlogConstants.updateBom, updateBom);
            return viewBom.withInit(bom.getId());
        } catch (Throwable e) {
            if (e instanceof DataAccessException) {
                e = e.getCause().getCause();
            }
            throw AppRuntimeException.rethrow("更新单板表错误", e);
        }
    }
}
